package com.wandian.news.adapter;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.news.R;
import com.wandian.news.bean.TouTiaoContentBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

/**
 * @author Administrator
 * @date 2019/12/16-13:32
 * @description 适配器
 **/
public class TouTiaoDetailsRecycleAdapter extends BaseQuickAdapter<TouTiaoContentBean.NewslistBean, BaseViewHolder> {

    private String selectId;
    private List<TTNativeExpressAd> ads = new ArrayList<>();
    public TouTiaoDetailsRecycleAdapter(int layoutResId, String selectId) {
        super(layoutResId);
        this.selectId = selectId;
    }
    public List<TTNativeExpressAd> getAds() {
        return ads;
    }

    public void setAds(List<TTNativeExpressAd> ads) {
        this.ads = ads;
    }

    public String getSelectId() {
        return selectId;
    }

    public void setSelectId(String selectId) {
        this.selectId = selectId;
    }

    public ItemDataClink itemClink;
    public interface ItemDataClink{
        void itemViewclink(String news_form_time, String newsID, String newsTitle, String newsDescribe, String value);
    }
    public void setItemDAtaClink(ItemDataClink itemClink){
        this.itemClink = itemClink;
    }


    @Override
    protected void convert(BaseViewHolder helper, TouTiaoContentBean.NewslistBean item) {
        RelativeLayout rl_item = helper.getView(R.id.rl_item);
        Log.d("toutiao_ad",""+helper.getLayoutPosition());
        try {
            rl_item.setVisibility(View.VISIBLE);
            ImageView iv_zx_1 = helper.getView(R.id.iv_zx_1);
            if(!UIUtils.isEmpty(item.getPicUrl())){
                GlideUtil.intoNewsPic(mContext,item.getPicUrl(),iv_zx_1);
            }
            TextView tv_time = helper.getView(R.id.tv_time);
            TextView tv_form = helper.getView(R.id.tv_form);
            TextView tv_zx_title = helper.getView(R.id.tv_zx_title);
            if(!UIUtils.isEmpty(item.getCtime())){
                if(item.getCtime().length()>10){
                    tv_time.setText(""+item.getCtime().substring(0,10));
                }else {
                    tv_time.setText(""+item.getCtime());
                }
            }
            if(!UIUtils.isEmpty(item.getDescription())){
                tv_form.setText(item.getDescription()+"");
            }
            if(!UIUtils.isEmpty(item.getTitle())){
                tv_zx_title.setText(item.getTitle());
            }

            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!UIUtils.isEmpty(item.getUrl())){
                        if(!UIUtils.isEmpty(item.getUrl())){
                            itemClink.itemViewclink(item.getDescription()+"  "+item.getCtime(),item.getUrl(),
                                    item.getTitle(),item.getDescription(),"新闻详情");
                        }
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!UIUtils.isEmpty(selectId) && selectId.equals(item.getUrl())){
            if(!UIUtils.isEmpty(ads) && ads.size()>0){
                try {
                    int index = 0;
                    Random random = new Random();
                    index =random.nextInt(ads.size());
                    CSJADTools.bindAdListener(ads.get(index),rl_item);
                    ads.get(index).render();
                    notifyItemChanged(helper.getLayoutPosition());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }else {
            if(helper.getLayoutPosition()>0 && helper.getLayoutPosition()%3==0){
                Log.d("toutiao_ad","===>>"+helper.getLayoutPosition());
                if(!UIUtils.isEmpty(ads) && ads.size()>0){
                    try {
                        int index = 0;
                        Random random = new Random();
                        index =random.nextInt(ads.size());
                        CSJADTools.bindAdListener(ads.get(index),rl_item);
                        ads.get(index).render();
                        notifyItemChanged(helper.getLayoutPosition());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }



    }

}
